<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.categoryId" clearable placeholder="请选择分类">
          <el-option
            v-for="category in categoryList"
            :key="category.id"
            :label="category.categoryName"
            :value="category.id">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="文章id" align="center" prop="id"/>
      <el-table-column label="创建时间" align="center" prop="createDate">
        <template slot-scope="scope">
          {{ scope.row.createDate | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="是否置顶" align="center" prop="weight">
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.weight"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="5"
            :inactive-value="0"
            @change="handleChangeStatus($event,scope.row.id)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="作者账号" align="center" prop="user.account"/>
      <el-table-column label="分类" align="center" prop="category.categoryName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:article:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listArticle, getArticle, delArticle, addArticle, updateArticle, updateStatus} from "@/api/blog/article";
import {listCategory} from "@/api/blog/category";

export default {
  name: "Article",
  data() {
    return {
      categoryList: [],
      // value: '',
      // value: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        categoryId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        weight: [
          {required: true, message: "是否置顶不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    //获取分类列表list
    getCategoryList() {
      listCategory(null).then(response => {
        console.log(response)
        this.categoryList = response.rows;
      })
    },
    //切换文章置顶状态
    handleChangeStatus($event, id) {
      if ($event == 5) {
        this.$modal.confirm('是否确认置顶编号为"' + id + '"的文章？').then(function () {
          return updateStatus(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("置顶成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否取消置顶编号为"' + id + '"的文章？').then(function () {
          return updateStatus(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消成功");
        }).catch(() => {
        });
      }
    },
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        commentCounts: null,
        createDate: null,
        summary: null,
        title: null,
        viewCounts: null,
        weight: null,
        authorId: null,
        bodyId: null,
        categoryId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章编号为"' + ids + '"的数据项？').then(function () {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
